package ac.neec.mio.ui.fragment;

import ac.neec.listener.EventsCallBackListener;
import ac.neec.listener.HeartRateEventListener;
import ac.neec.mio.R;
import ac.neec.mio.ui.activity.MeasurementTestActivity;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

/**
 * Created by kobayashi on 2014/01/26.
 */
public class MeasurementTestFlagment extends Fragment implements
		HeartRateEventListener<String> {
	private static final int HANDLE_ID_HEART_RATE = 0;
	private static final String HEART_RATE_EMPTY_MESSAGE = "---";
	private boolean measurementFlag = false;
	private boolean measurementInitFlag = false;
	private TextView deviceName;
	private TextView deviceAddr;
	private TextView heartRate;
	private Button startButton;
	private Button stopButton;
	private EventsCallBackListener<Object> mCallbackListener;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case HANDLE_ID_HEART_RATE:
				if (measurementFlag)
					heartRate.setText((String) msg.obj);
				else
					heartRate.setText(HEART_RATE_EMPTY_MESSAGE);
				break;
			default:
				break;
			}
		}
	};
	private OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			if (v == startButton) {
				startMeasurement();
			} else if (v == stopButton) {
				stopMeasuremenr();
			}
		}
	};

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mCallbackListener = (EventsCallBackListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnArticleSelectedListener");
		}
	}

	@Override
	public void onCreate(Bundle savedStateInstance) {
		super.onCreate(savedStateInstance);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedStateInstance) {
		View view = inflater.inflate(R.layout.fragment_test_measurement,
				container, false);

		deviceName = (TextView) view
				.findViewById(R.id.frag_measurement_test_connect_device_name);
		deviceAddr = (TextView) view
				.findViewById(R.id.frag_measurement_test_connect_device_mac_addr);
		heartRate = (TextView) view
				.findViewById(R.id.frag_measurement_test_heart_rate_text);
		startButton = (Button) view
				.findViewById(R.id.frag_measurement_test_button_start);
		stopButton = (Button) view
				.findViewById(R.id.frag_measurement_test_button_stop);
		startButton.setOnClickListener(mOnClickListener);
		stopButton.setOnClickListener(mOnClickListener);
		stopButton.setEnabled(false);

		Bundle  bundle = getArguments();
		deviceName.setText(bundle.getString("deviceName"));
		deviceAddr.setText(bundle.getString("deviceAddr"));

		return view;
	}

	public void startMeasurement() {
		if (measurementInitFlag == false) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			mCallbackListener.onCallBack(MeasurementTestActivity.CALLBACK_ID_MEASUREMENT_START, null);
			measurementInitFlag = true;
		}
		measurementFlag = true;
		startButton.setEnabled(false);
		stopButton.setEnabled(true);
	}

	public void stopMeasuremenr() {
		measurementFlag = false;
		stopButton.setEnabled(false);
		startButton.setEnabled(true);
	}

	@Override
	public void notifyHeartRateEvent(String value) {
		mHandler.obtainMessage(HANDLE_ID_HEART_RATE, value).sendToTarget();
	}

}
